﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTableManagement.Entities;
using TimeTableManagement;
using System.Data.OleDb;
using System.Web.Script.Serialization;

namespace TimeTableManagement.Controllers
{
    [Authorize]
    public class EducationController : Controller
    {
        private QLSVContext db = new QLSVContext();

        public static int getTermStart(int id_edu)
        {
            QLSVContext _db = new QLSVContext();
            Tb_Education edu = _db.Tb_Education.Find(id_edu);
            var con = _db.Tb_Config.Where(g => g.keyword.Equals(edu.code)).Where(g=>g.option.Equals("termstart"));
            int ts = con.First<Tb_Config>().id;
            _db.Dispose();
            return ts;
        }

        //
        // GET: /Education/

        public ViewResult Index()
        {
            var tb_education = db.Tb_Education.Include(t => t.Tb_Department);
            return View(tb_education.ToList());
        }

        //
        // GET: /Education/Details/5

        public ViewResult Details(int id)
        {
            Tb_Education tb_education = db.Tb_Education.Find(id);
            return View(tb_education);
        }

        //
        // GET: /Education/Create

        public ActionResult Create()
        {
            ViewBag.department = new SelectList(db.Tb_Department, "id", "name");
            ViewBag.termstart = new SelectList(db.Tb_Config.Where(g => g.option.Equals("term")), "id", "displayword");
            return View();
        } 

        //
        // POST: /Education/Create

        [HttpPost]
        public ActionResult Create(Tb_Education tb_education, string termstart)
        {
            if (ModelState.IsValid)
            {
                db.Tb_Education.Add(tb_education);
                db.SaveChanges();
                Tb_Config con = new Tb_Config();
                con.keyword = tb_education.code;
                con.displayword = tb_education.code;
                con.value = termstart;
                con.option = "termstart";
                db.Tb_Config.Add(con);
                QLSVContext a = new QLSVContext();
                a.Tb_Config.Add(con);
                a.SaveChanges();
                a.Dispose();
                return RedirectToAction("Index");  
            }

            ViewBag.department = new SelectList(db.Tb_Department, "id", "name", tb_education.department);
            return View(tb_education);
        }
        
        //
        // GET: /Education/Edit/5
 
        public ActionResult Edit(int id)
        {
            Tb_Education tb_education = db.Tb_Education.Find(id);
            ViewBag.department = new SelectList(db.Tb_Department, "id", "name", tb_education.department);
            Tb_Config  con = db.Tb_Config.Where(g => g.option.Equals("termstart"))
                .Where(g => g.keyword == tb_education.code).First();
            ViewBag.termstart = new SelectList(db.Tb_Config.Where(g => g.option.Equals("term")), "id", "displayword",con.value);
            String listedu = tb_education.listcourse;
            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<EduList> listeducation = jss.Deserialize<List<EduList>>(listedu);
            ViewBag.listcourse = listeducation;
            return View(tb_education);
        }

        //
        // POST: /Education/Edit/5

        [HttpPost]
        public ActionResult Edit(Tb_Education tb_education, string termstart)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_education).State = EntityState.Modified;
                db.SaveChanges();
                Tb_Config conf = db.Tb_Config
                    .Where(g => g.keyword.Equals(tb_education.code))
                    .Where(g => g.option.Equals("termstart")).First();
                QLSVContext a = new QLSVContext();
                Tb_Config x = a.Tb_Config.Find(conf.id);
                x.value = termstart;
                a.SaveChanges();
                a.Dispose();
                return RedirectToAction("Index");
            }
            ViewBag.department = new SelectList(db.Tb_Department, "id", "name", tb_education.department);
            Tb_Config con = db.Tb_Config.Where(g => g.option.Equals("termstart"))
                .Where(g => g.keyword == tb_education.code).First();
            ViewBag.termstart = new SelectList(db.Tb_Config.Where(g => g.option.Equals("term")), "id", "displayword", con.value);
            String listedu = tb_education.listcourse;
            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<EduList> listeducation = jss.Deserialize<List<EduList>>(listedu);
            ViewBag.listcourse = listeducation;
            return View(tb_education);
        }

        //
        // GET: /Education/Delete/5
 
        public ActionResult Delete(int id)
        {
            Tb_Education tb_education = db.Tb_Education.Find(id);
            return View(tb_education);
        }

        //
        // POST: /Education/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tb_Education tb_education = db.Tb_Education.Find(id);
            db.Tb_Education.Remove(tb_education);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Copy(int id)
        {
            Tb_Education tb_education = db.Tb_Education.Find(id);
            ViewBag.department = new SelectList(db.Tb_Department, "id", "name", tb_education.department);
            ViewBag.termstart = new SelectList(db.Tb_Config.Where(g => g.option.Equals("term")), "id", "displayword");
            String listedu = tb_education.listcourse;
            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<EduList> listeducation = jss.Deserialize<List<EduList>>(listedu);
            ViewBag.listcourse = listeducation;
            return View();
        }

        [HttpPost]
        public ActionResult List(String listedu)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<EduList> listeducation = jss.Deserialize<List<EduList>>(listedu);
            return View(listeducation);
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}